package org.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.common.Result;
import org.example.pojo.Category;
import org.example.pojo.Comment;
import org.example.pojo.Order;
import org.example.pojo.Query;
import org.example.service.CommentService;
import org.example.mapper.CommentMapper;
import org.example.service.OrderService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author 13326
* @description 针对表【hards_comment】的数据库操作Service实现
* @createDate 2025-01-12 09:49:10
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
@Resource
private CommentMapper commentMapper;
@Resource
private OrderService orderService;
    @Override
    public Result add(Comment comment){
        comment.setTime(DateUtil.now());
    int rows=commentMapper.insert(comment);
        Order order=new Order();
        order.setId(comment.getOrderId());
        order.setStatus("已完成");
    orderService.updateorder(order);
    return Result.ok("评价成功");
}
@Override
    public Result selectlist(Query query){
    LambdaQueryWrapper<Comment> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper
            .like(!StringUtils.isEmpty(query.getKeyword()), Comment::getContent,query.getKeyword())
            .eq(Comment::getContent,query.getKeyword());
    IPage<Comment> page=new Page<>(query.getPageNum(),query.getPageSize());
   commentMapper.selectPageMap(page,query);
    HashMap<String,Object> map=new HashMap<>();
    map.put("total",page.getTotal());
    map.put("rows",page.getRecords());
    map.put("pages",page.getPages());
    map.put("size",page.getSize());
    return Result.ok(map);


}
@Override
    public Result shanchu(Integer id){
        commentMapper.deleteById(id);
        return Result.ok("删除成功");
}
@Override
    public Result all(Integer userId){
LambdaQueryWrapper<Comment> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(Comment::getUserId,userId);
List<Comment> commentList=commentMapper.selectList(lambdaQueryWrapper);
return Result.ok(commentList);
}
}




